package com.example.tests;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import com.example.fw.datamapping.ContactLine;

public class ContactData {
	private String fName;
	private String lName;
	private String prmAddress;
	private String prmPhone;
	private String mobPhone;
	private String wrkPhone;
	private String prmEmail;
	private String secEmail;
	private Calendar brthDay;
	private String secAddress;
	private String secPhone;
	private String group;

	private static SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); 
	
	public ContactData() {
	//TODO: revert back initiation with null values. -- need for contact modification tests: 
	//null -- leave the field without any changes, "" -- clear the field 
/*		this.fName = "";
		this.lName = "";
		this.prmAddress = "";
		this.prmPhone = "";
		this.mobPhone = "";
		this.wrkPhone = "";
		this.prmEmail = "";
		this.secEmail = "";
		this.brthDay = null;
		this.secAddress = "";
		this.secPhone = "";
		this.group = "";
*/
	}

	public ContactLine toContactLine(){
		ContactLine contactLine = new ContactLine();
		
		contactLine.fName = ((this.fName == null)? "" : this.fName.trim().replaceAll(" +", " "));
		contactLine.lName = ((this.lName == null)? "" : this.lName.trim().replaceAll(" +", " "));
		
		if ((this.prmEmail != null)&&(this.prmEmail.length()>0)){
			contactLine.email = this.prmEmail.trim().replaceAll(" +", " ");
		} else {
			contactLine.email = ((this.secEmail == null)? "" : this.secEmail.trim().replaceAll(" +", " "));
		}
		
		if ((this.prmPhone != null)&&(this.prmPhone.length()>0)){
			contactLine.phone = this.prmPhone.replaceAll("\\s",""); 
		} else if ((this.mobPhone != null)&&(this.mobPhone.length()>0)) {
			contactLine.phone = this.mobPhone.replaceAll("\\s",""); 
		} else {
			contactLine.phone = ((this.wrkPhone == null)? "" : this.wrkPhone.replaceAll("\\s",""));
		}
		
		contactLine.isPrimaryAddSet = ((this.prmAddress == null) ? false : (this.prmAddress.length()>0));
		
		return contactLine;
	}

	public ContactData updateWithOtherContact (ContactData other){
		// if a field in other object is null -- leave current value
		ContactData newContact = new ContactData()
			.withFName((other.fName == null) ?  this.fName : other.fName)
			.withLName((other.lName == null) ?  this.lName : other.lName)
			.withPrmAddress((other.prmAddress == null) ?  this.prmAddress : other.prmAddress)
			.withPrmPhone((other.prmPhone == null) ?  this.prmPhone : other.prmPhone)
			.withMobPhone((other.mobPhone == null) ?  this.mobPhone : other.mobPhone)
			.withWrkPhone((other.wrkPhone == null) ?  this.wrkPhone : other.wrkPhone)
			.withPrmEmail((other.prmEmail == null) ?  this.prmEmail : other.prmEmail)
			.withSecEmail((other.secEmail == null) ?  this.secEmail : other.secEmail)
			.withBrthDay((other.brthDay == null) ?  this.brthDay : other.brthDay)
			.withSecAddress((other.secAddress == null) ?  this.secAddress : other.secAddress)
			.withSecPhone((other.secPhone == null) ?  this.secPhone : other.secPhone);
		
		return newContact;
	}
	
	public String getFName() {
		return fName;
	}

	public ContactData withFName(String fName) {
		this.fName = fName;
		return this;
	}

	public String getLName() {
		return lName;
	}

	public ContactData withLName(String lName) {
		this.lName = lName;
		return this;
	}

	public String getPrmAddress() {
		return prmAddress;
	}

	public ContactData withPrmAddress(String prmAddress) {
		this.prmAddress = prmAddress;
		return this;
	}

	public String getPrmPhone() {
		return prmPhone;
	}

	public ContactData withPrmPhone(String prmPhone) {
		this.prmPhone = prmPhone;
		return this;
	}

	public String getMobPhone() {
		return mobPhone;
	}

	public ContactData withMobPhone(String mobPhone) {
		this.mobPhone = mobPhone;
		return this;
	}

	public String getWrkPhone() {
		return wrkPhone;
	}

	public ContactData withWrkPhone(String wrkPhone) {
		this.wrkPhone = wrkPhone;
		return this;
	}

	public String getPrmEmail() {
		return prmEmail;
	}

	public ContactData withPrmEmail(String prmEmail) {
		this.prmEmail = prmEmail;
		return this;
	}

	public String getSecEmail() {
		return secEmail;
	}

	public ContactData withSecEmail(String secEmail) {
		this.secEmail = secEmail;
		return this;
	}

	public ContactData withBrthDay(Calendar brthDay) {
		if (brthDay != null) { 
			this.withBrthDay(brthDay.getTime());
		} else {
			this.brthDay = null;
		}
		return this;
	}

	public ContactData withBrthDay(String brthDay) throws ParseException {
		if (brthDay != null) { 
			this.withBrthDay(sdf.parse(brthDay));
		} else {
			this.brthDay = null;
		}
		return this;
	}

	public ContactData withBrthDay(Date brthDay) {
		if (brthDay != null) { 
			this.brthDay = new GregorianCalendar();
			this.brthDay.setTime(brthDay);
		} else {
			this.brthDay = null;
		}
		return this;
	}

	public Calendar getBrthDayCal() {
		if (this.brthDay != null){
			return this.brthDay;
		} else {
			return null;
		}
	}
	
	public String getBrthDayStr() {
		if (this.brthDay != null){
			return sdf.format(this.brthDay.getTime()) ;
		} else {
			return null;
		}
	}
	
	public Integer getBrthDay() {
		if (this.brthDay != null){
			return this.brthDay.get(Calendar.DAY_OF_MONTH);
		} else {
			return null;
		}
	}

	public Integer getBrthMonth() {
		if (this.brthDay != null){
			return (this.brthDay.get(Calendar.MONTH)+1);
		} else {
			return null;
		}
	}

	public Integer getBrthYear() {
		if (this.brthDay != null){
			return this.brthDay.get(Calendar.YEAR);
		} else {
			return null;
		}
	}	
	
	public String getSecAddress() {
		return secAddress;
	}

	public ContactData withSecAddress(String secAddress) {
		this.secAddress = secAddress;
		return this;
	}

	public String getSecPhone() {
		return secPhone;
	}

	public ContactData withSecPhone(String secPhone) {
		this.secPhone = secPhone;
		return this;
	}

	public String getGroup() {
		return group;
	}
	
	public ContactData withGroup(String group){
		this.group = group;
		return this;
	}
}